package mail_operations;

import java.io.File;
import java.util.ArrayList;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;


import data.ConfigInfo;
import data.EmployeeInfo;
import data.Reference;


public class WorkbookMailer {

	public static void sendMailByOrigin(ArrayList<String> wbList, String rptName) throws AddressException{
		for(int wbCount=0; wbCount<wbList.size();wbCount++){
			String wbName = wbList.get(wbCount);
			File file = new File(wbName);
			String fileN = file.getName();
			String subject = fileN.substring(0, fileN.indexOf(".xls"));
			String cneeName = fileN.substring(fileN.indexOf(")-")+2, fileN.indexOf(" " + rptName));
			String originName = fileN.substring(fileN.indexOf("(")+1, fileN.indexOf(")-"));		
			String receiver = EmployeeInfo.getEmployeeEmail(originName, cneeName);
			ArrayList<InternetAddress> ccList = new ArrayList<InternetAddress>();
			for (int k=0;k<ConfigInfo.ADDITIONAL_EMAIL_LIST.size();k++)
				ccList.add(InternetAddress.parse(ConfigInfo.ADDITIONAL_EMAIL_LIST.get(k))[0]);

			if(subject.contains("Reminder")){
				for (int k=0;k<ConfigInfo.EMAIL_SECOND_REMINDER.size();k++)
					ccList.add(InternetAddress.parse(ConfigInfo.EMAIL_SECOND_REMINDER.get(k))[0]);
			}
			if(subject.contains("3rd Reminder")){
				for (int k=0;k<ConfigInfo.EMAIL_THIRD_REMINDER.size();k++)
					ccList.add(InternetAddress.parse(ConfigInfo.EMAIL_THIRD_REMINDER.get(k))[0]);
			}
			String signature = 
					"Best Regards,\n" +
					ConfigInfo.SENDER_NAME + "\n" + "\n"+
					"Data Management - LAX\n"+ 
					"2727 Workman Mill Rd.\n"+
					"City of Industry, CA 90601\n"+
					"Tel: 562-908-1688 EXT:" + ConfigInfo.SENDER_EXT + "\n"+
					"Tax: 562-908-1699\n"+
					"Email: " + ConfigInfo.SENDER_EMAIL + "\n"+
					"Website: www.topocean.com";
			String body = 
					"Dear " + originName + " Team,\n" + "\n" +
					"Please see attached for today's " + cneeName + " missing document report.\n" + "\n" +
					"Thanks.\n" + "\n" +
					signature;
			//If email sending for CYPRESS MEDICAL
			if (Reference.reportNo==1){
				body = 
						"Dear " + originName + " Team,\n" + "\n" +
						"Please see attached for today's " + cneeName + " missing document report.\n" + "\n" +
						"Every shipment MUST include all the required documents WITHIN the set timeframe that list on SOP. " +
						"If any vendor refuse to send the document(s), please notice Christie To to follow up with the vendor." +
						"Thanks.\n" + "\n" +
						signature;
			//If email for Late PO
			}else if(Reference.reportNo==3){
				body = 
						"Dear " + originName + " Team,\n" + "\n" +
						"Please see attached for today's " + cneeName + " unscheduled PO report.\n" + "\n" +
						"Thanks.\n" + "\n" +
						signature;
			}else if(Reference.reportNo==4){
				body = 
						"Dear " + originName + " Team,\n" + "\n" +
						"Please see attached for today's " + cneeName + " missing contract type document.\n" + "\n" +
						"Thanks.\n" + "\n" +
						signature;
			}
			
			ArrayList<String> errorlog = mail(receiver, ccList.toArray(), subject, body, file);
			for(int printE = 0;printE<errorlog.size();printE++){
				System.out.println(errorlog.get(printE));
			}
		}
	}	
	public static ArrayList<String> mail(String receiver, Object[] ccList, String subject, String body, File fileName){
		
		//temp fields for testing
		return ReportMailer.mail(ConfigInfo.SERVER_ADDRESS, ConfigInfo.SENDER_EMAIL, 
				ccList, receiver, subject, body, fileName);
	}
}
